package com.ar4j.type;

import java.sql.Types;

/**
 * Converter class providing ability to convert from an Enum to its string name and back.
 */
public class EnumTypeConverter<E extends Enum<E>> implements ITypeConverter<E, String> {
  
  @Override
  public String convertToStorage(E value) {
    return (value == null ? null : value.name());
  }

  @Override
  public E convertFromStorage(String storageValue, Class<E> objectType) {
    return (storageValue == null ? null : Enum.valueOf(objectType, storageValue));
  }

  @Override
  public int getSqlType() {
    return Types.VARCHAR;
  }
  
  @Override
  public Class<String> getStorageType() {
    return String.class;
  }

}
